package telsoft.app.bean;

import java.sql.*;
import java.util.*;
import telsoft.admin.util.*;
import telsoft.sql.*;
import telsoft.app.util.*;
import telsoft.util.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ISDNBean extends AppEntityProcessor {
    private String mstrISDN = "";
    private String mstrIMSI = "";
    private Integer isuccess = 0;
    private Vector vtData = new Vector();
    private Vector vtErrData2 = new Vector();
//    private Vector vtRow = new Vector();
    String strSQL;
    PreparedStatement stmt = null;

    public Vector loadForm() throws Exception {
        try {
            open();
	    strSQL = "select ISDN,IMSI,cus_name, to_char(cus_birthday,'dd/mm/yyyy'),cus_address, to_char(reg_date,'dd/mm/yyyy HH24:MI:SS'),case"+
		     " WHEN reg_status='1' then 'Đã kích hoạt'"+
		     " else 'Chưa kích hoạt'"+
		     " end"+
		     " from ISDN ";
            Vector vtReturn = Database.executeQuery(getConnection(), strSQL);
            return vtReturn;//tra ve vector de day ve client
        } finally {
            close();
        }
    }

    public void onInsert() throws Exception {
        PreparedStatement stmt = null;
        String strSQL = "INSERT INTO ISDN( " +
                        "ISDN, IMSI,reg_status) " +
                        "VALUES (?,?,0) ";

        try {
            loadParameter();
//	    mstrID = Database.getSequenceValue(mcnMain,"seq_payment");
            stmt = mcnMain.prepareStatement(strSQL);
            stmt.setString(1, mstrISDN);
            stmt.setString(2, mstrIMSI);
            stmt.executeUpdate();
            log.logAfterInsert(mstrLogID, "ISDN",
                               "ISDN='" +
                               StringEscapeUtil.escapeSql(mstrISDN) + "'");
            storeParameter();
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(e, "ISDNBean.insert");
        } finally {
            Database.closeObject(stmt);
        }
    }


    public void loadParameter() { //ham lay ve cac gia tri nguoi dung nhap tren form
        mstrISDN = StringUtil.nvl(request.getString("mstrISDN"), ""); //so thue bao
        mstrIMSI = StringUtil.nvl(request.getString("mstrIMSI"), ""); //so imsi
        vtData = request.getVector("mvtData"); //lay ve vector du lieu
        if (vtData == null) {
            vtData = new Vector();
        }
    }


    public void storeParameter() {
        response.setString("mstrISDN", mstrISDN);
        response.setString("mstrIMSI", mstrIMSI);
    }

    protected String getModuleName() {
        return PermissionApp.FormISDN;
    }

    public void importData() throws SQLException {
        try {
            open();
            mcnMain.setAutoCommit(false);
	    boolean bCheckError = true;
            //insert du lieu
            loadParameter();
            for (int i = 0; i < vtData.size(); i++) {
                PreparedStatement stmt = null;
                Vector vtRow = (Vector) vtData.elementAt(i);
		Vector vtErrData = new Vector();
                String strSQL = "INSERT INTO ISDN " +
                                "(ISDN,IMSI,reg_status)" +
                                "VALUES (?,?,0) ";

                try {
//	    mstrID = Database.getSequenceValue(mcnMain,"seq_payment");
                    stmt = mcnMain.prepareStatement(strSQL);
                    stmt.setString(1, vtRow.elementAt(0).toString());
                    stmt.setString(2, vtRow.elementAt(1).toString());
                    stmt.executeUpdate();
                    log.logAfterInsert(mstrLogID, "ISDN",
                                       "ISDN='" +
                                       StringEscapeUtil.escapeSql(mstrISDN) +
                                       "'");
                    isuccess++;
                } catch (Exception e) {
                    vtErrData.addElement(e.getMessage());
                    vtErrData.addElement(i);
                    vtErrData.addElement(vtRow.elementAt(0).toString());
                    vtErrData.addElement(vtRow.elementAt(1).toString());
		    vtErrData2.addElement(vtErrData);
		    bCheckError = false;
                }
            }
	    if(!bCheckError)
	    {
		isuccess = 0;
		throw new Exception("Error occur : Rollback");
	    }
            mcnMain.commit();
        } catch (Exception e) {
            mcnMain.rollback();//xoa het cac tac vu vua thao tac neu co ban ghi loi xay ra
            e.printStackTrace();
        } finally {
            mcnMain.setAutoCommit(true);
            response.setInt("isuccess", isuccess);
	    response.setVector("vtErrData2",vtErrData2);
            close();
        }

    }

}
